import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { ActivitiesType, CurrentUser, NoticeType, RadarDataType } from './data.d';
import { queryAmtdetailData, queryOrderYearNumData, queryYearExportvolumeData, queryYearRecamountData } from './service';

export interface ModalState {
  currentUser: Partial<CurrentUser>;
  amtdetailData: NoticeType[];
  queryYearRecamountData: ActivitiesType[];
  queryAmtdetailData: RadarDataType[];
}

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: ModalState) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: ModalState;
  reducers: {
    save: Reducer<ModalState>;
    clear: Reducer<ModalState>;
  };
  effects: {
    init: Effect;
    fetchAmtdetailData: Effect;
    fetchQueryYearExportvolumeData: Effect;
    fetchQueryYearRecamountData: Effect;
    fetchUserCurrent: Effect;
    fetchOrderYearNumData: Effect,
  };
}

const Model: ModelType = {
  namespace: 'dashboardWorkplace',
  state: {
    currentUser: {},
    amtdetailData: [],
    yearExportvolumeData: [],
    yearRecamountData: [],
    orderYearNumData: [],
  },
  effects: {
    *init(_, { put }) {
      yield put({ type: 'fetchAmtdetailData' });
      yield put({ type: 'fetchQueryYearExportvolumeData' });
      yield put({ type: 'fetchQueryYearRecamountData' });
      yield put({ type: 'fetchOrderYearNumData' });
      yield put({ type: 'fetchUserCurrent' });
    },
    *fetchUserCurrent(_, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          currentUser: {
            name: sessionStorage.getItem('userName'),
            avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
            userid: '00000001',
            email: 'antdesign@alipay.com',
            signature: '海纳百川，有容乃大',
            title: '贵宾客户【VIP】',
            group: sessionStorage.getItem('userName'),
          },
        },
      });
    },
    *fetchAmtdetailData(_, { call, put }) {
      const response = yield call(queryAmtdetailData);
      yield put({
        type: 'save',
        payload: {
          amtdetailData: Array.isArray(response.content) ? response.content : [],
        },
      });
    },
    *fetchOrderYearNumData({ payload }, { call, put }) {
      const response = yield call(queryOrderYearNumData, payload);
      yield put({
        type: 'save',
        payload: {
          orderYearNumData: Array.isArray(response.content) ? response.content : [],
        },
      });
    },
    *fetchQueryYearExportvolumeData({ payload }, { call, put }) {
      const response = yield call(queryYearExportvolumeData, payload);
      yield put({
        type: 'save',
        payload: {
          yearExportvolumeData: Array.isArray(response.content) ? response.content : [],
        },
      });
    },
    *fetchQueryYearRecamountData({ payload }, { call, put }) {
      const response = yield call(queryYearRecamountData, payload);
      yield put({
        type: 'save',
        payload: {
          yearRecamountData: Array.isArray(response.content) ? response.content : [],
        },
      });
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clear() {
      return {
        currentUser: {},
        amtdetailData: [],
        yearExportvolumeData: [],
        yearRecamountData: [],
        orderYearNumData: [],
      };
    },
  },
};

export default Model;
